<?php
namespace Application\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Debug\Debug;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;
class ProductTable{
	protected $tableGateway;

	public function __construct(TableGateway $table){
		$this->tableGateway = $table;
	}

	public function fetchAll(){
		$adapter = $this->getAdapter();
		$sql = new Sql($adapter);
		$select = $sql->select('sanpham');
		$select->columns(array('hinhAnh','maHang','id','moTa','gia','soLuong','loaiHang','ten'));
		$select->limit(10);
		$selectString = $sql->getSqlStringForSqlObject($select);
		$results = $adapter->query($selectString,$adapter::QUERY_MODE_EXECUTE);
		$result = $results->toArray();
		return $result;
	}

	// lấy thông tin một sản phẩm
	public function getProduct($id){
		$id = (int)$id;
		$adapter = $this->getAdapter();
		$sql = new Sql($adapter);
		$select = $sql->select();
		$select->columns(array('maHang','hinhAnh','soLuong','moTa','loaiHang','gia','ten'));
		$select->from('sanpham');
		$select->where(array('id'=>$id));
		$selectString = $sql->getSqlStringForSqlObject($select);
		$results = $adapter->query($selectString,$adapter::QUERY_MODE_EXECUTE);
		$result = $results->current();
		return $result;
	}

	public function getAdapter(){
		$adapter = new Adapter(array(
		    'driver' => 'Pdo_Mysql',
		    'database' => 'giayvinhxinh',
		    'username' => 'root',
		    'password' => '',
		    'driver_options'=>  array(
				1002=>'SET NAMES utf8'
			),
		 ));
		return $adapter;
	}

	public function getReleventItems($id, $type){

	}	

	public function getTopSellers(){
		
	}
}